package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.vo.OneSubjectVO;
import com.atguigu.eduservice.entity.vo.TwoSubjectVO;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exception.EduException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-11
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    // 读取Excel
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream ins = file.getInputStream();
            EasyExcel.read(ins, SubjectData.class, new SubjectExcelListener(eduSubjectService))
                    .sheet().doRead();
        } catch (Exception e) {
            throw new EduException("添加课程分类失败", 20002);
        }
    }

    /**
     * 课程分类列表树形
     *
     * @return
     */
    @Override
    public List<OneSubjectVO> getAllOneTwoSubject() {
        // 查询所有一级分类  pid=0
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjects = baseMapper.selectList(queryWrapper);
        // 查询所有二级分类  pid!=0
        QueryWrapper<EduSubject> twoQueryWrapper = new QueryWrapper<>();
        twoQueryWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjects = baseMapper.selectList(twoQueryWrapper);
        // 创建List集合 存储最终封装数据
        List<OneSubjectVO> finalSubjects = new ArrayList<>();
        // 封装一级分类
        // 把查询出来的所有一级分类遍历 得到每个一级分类对象 获得值
        for (int i = 0; i < oneSubjects.size(); i++) {
            // 把查询出来的所有一级分类遍历 得到每个一级分类对象 获得值
            EduSubject oneEduSubject = oneSubjects.get(i);
            // 创建空的OneSubjectVO对象
            OneSubjectVO oneSubjectVO = new OneSubjectVO();
            // 使用工具类复制属性值
            BeanUtils.copyProperties(oneEduSubject, oneSubjectVO);
            // 封装到要求的List集合里面List<OneSubjectVO> finalSubject
            finalSubjects.add(oneSubjectVO);
            // 遍历取出来的二级分类
            // 创建存储二级分类的空集合用来封装每个一级分类的二级分类
            List<TwoSubjectVO> twoSubjectVOS = new ArrayList<>();
            for (int m = 0; m < twoSubjects.size(); m++) {
                // 得到每一个二级分类对象
                EduSubject twoEduSubject = twoSubjects.get(m);
                // 判断二级分类 二级分类的pid 等于一级分类的id
                // 这个地方不建议直接用twoEduSubject.getParentId().equals()方法可能会出现空指针
                if (Objects.equals(twoEduSubject.getParentId(), oneEduSubject.getId())) {
                    // 创建空集合
                    TwoSubjectVO twoSubjectVO = new TwoSubjectVO();
                    BeanUtils.copyProperties(twoEduSubject, twoSubjectVO);
                    twoSubjectVOS.add(twoSubjectVO);
                }

            }
            oneSubjectVO.setChildren(twoSubjectVOS);
        }

        // 返回最终的集合
        return finalSubjects;
    }
}
